package com.xiashitech.apiservice.api;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.xiashitech.apiservice.Constants.ErrorCode;
import com.xiashitech.apiservice.dto.DesenseRuleDTO;
import com.xiashitech.apiservice.dto.DesenseRuleResponse;
import com.xiashitech.apiservice.service.IDesenseRule;
import com.xiashitech.interfaces.apiservice.dto.base.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * api脱敏规则的接口
 */
@Controller
@RequestMapping(value = "/v1/agent")
@RequiredArgsConstructor(onConstructor =@_(@Autowired))
public class DesenseRuleController {
    private static final Log log = LogFactory.get();

    private final IDesenseRule desenseRule;

    @RequestMapping(value = "/createDesenseRule", method = RequestMethod.POST, produces = "application/json;charset=UTF-8" ,consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public Response createDesenseRule(@RequestBody DesenseRuleDTO desenseRuleDTO) {
        if(desenseRuleDTO.getCode() == null || desenseRuleDTO.getName() == null || desenseRuleDTO.getRegular() == null || desenseRuleDTO.getReplaceSymbol() == null) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4014.getCode()).message(ErrorCode.RESULT_4014.getMessage()).build();
        }
        try {
            desenseRule.createDesenseRule(desenseRuleDTO);
        } catch (Exception e) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4001.getCode()).message(ErrorCode.RESULT_4001.getMessage()).build();
        }
        return DesenseRuleResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).build();
    }

    @RequestMapping(value = "/updateDesenseRule", method = RequestMethod.POST, produces = "application/json;charset=UTF-8" ,consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public Response updateDesenseRule(@RequestBody DesenseRuleDTO desenseRuleDTO) {
        if(desenseRuleDTO.getCode() == null) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4014.getCode()).message(ErrorCode.RESULT_4014.getMessage()).build();
        }
        try {
            desenseRule.updateDesenseRule(desenseRuleDTO);
        } catch (Exception e) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4001.getCode()).message(ErrorCode.RESULT_4001.getMessage()).build();
        }
        return DesenseRuleResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).build();
    }

    @RequestMapping(value = "/deleteDesenseRule", method = RequestMethod.POST, produces = "application/json;charset=UTF-8" ,consumes = "application/json;charset=UTF-8")
    @ResponseBody
    public Response deleteDesenseRule(@RequestBody DesenseRuleDTO desenseRuleDTO) {
        if(desenseRuleDTO.getCode() == null) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4014.getCode()).message(ErrorCode.RESULT_4014.getMessage()).build();
        }
        try {
            desenseRule.deleteDesenseRule(desenseRuleDTO);
        } catch (Exception e) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4001.getCode()).message(ErrorCode.RESULT_4001.getMessage()).build();
        }
        return DesenseRuleResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).build();
    }

    @RequestMapping(value = "/searchDesenseRule", method = RequestMethod.GET)
    @ResponseBody
    public Response searchDesenseRule(DesenseRuleDTO desenseRuleDTO) {
        if(desenseRuleDTO.getCode() == null && desenseRuleDTO.getName() == null) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4014.getCode()).message(ErrorCode.RESULT_4014.getMessage()).build();
        }
        try {
            List<DesenseRuleDTO> desenseRuleDTOS = desenseRule.searchDesenseRule(desenseRuleDTO);
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).desenseRuleDTOS(desenseRuleDTOS).build();
        } catch (Exception e) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4001.getCode()).message(ErrorCode.RESULT_4001.getMessage()).build();
        }
    }

    @RequestMapping(value = "/searchAllDesenseRule", method = RequestMethod.GET)
    @ResponseBody
    public Response searchAllDesenseRule() {
        try {
            List<DesenseRuleDTO> desenseRuleDTOS = desenseRule.searchDesenseRule(DesenseRuleDTO.builder().build());
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).desenseRuleDTOS(desenseRuleDTOS).build();
        } catch (Exception e) {
            return DesenseRuleResponse.builder().code(ErrorCode.RESULT_4001.getCode()).message(ErrorCode.RESULT_4001.getMessage()).build();
        }
    }
}
